/*
    Project:        Home Power Control
    File:           HPCLight.h
    Author:         christian@karunamusic.at
    Date:           02.2015
    Description:    Sensor to measure the ambient light with the TSL45315.
 */

#include <QString>
#include <wiringPiI2C.h>
#include <unistd.h>
#include <Logger/HPCLogger.h>
#include "HPCValue.h"
#include "HPCLight.h"

#define REG_CONTROL  0x00
#define REG_CONFIG   0x01
#define REG_DATALOW  0x04
#define REG_DATAHIGH 0x05
#define REG_ID       0x0A

HPCLight::HPCLight(const QString& name, quint32 id, quint8 i2caddr )
    : HPCSensor( TSensorType::HPCLight,name,id,i2caddr )
{
    m_I2C = wiringPiI2CSetup( m_Pin );
    // power on...
    wiringPiI2CWriteReg8(m_I2C,0x80|REG_CONTROL,0x03);
    // config...
    wiringPiI2CWriteReg8(m_I2C,0x80|REG_CONFIG,0x02);
}

HPCLight::~HPCLight()
{
    close(m_I2C);
}

QString HPCLight::asString() const
{
    QString ret = HPCSensor::asString();

    ret += " (i2c address)";
    return ret;
}

void HPCLight::Measure()
{
    HPCValue res;
    wiringPiI2CWrite(m_I2C,0x80|REG_DATALOW);
    res.V() = (unsigned int)wiringPiI2CRead(m_I2C) * 4;
    LOG( trace, Name() << ": measured value: " << res.V().toUInt() );
    m_Value = res;
}
